﻿using Data;
using System;
using System.Configuration;
using System.Data;

public class DataAccess
{

    private static DataManager dataManager
    {
        get
        {
            return new DataManager(ConfigurationManager.ConnectionStrings["Skala"].ConnectionString);
        }
    }


    public static DataTable GetUyeDogrulama(string EPosta,  string Parola)
    {
        DataTable dt = dataManager.ExecuteDataTable("GetUyeDogrulama", EPosta, Parola);
        return dt;
    }

    public static DataTable GetKullaniciDogrulama(string EPosta, string Parola)
    {
        DataTable dt = dataManager.ExecuteDataTable("GetKullaniciDogrulama", EPosta, Parola);
        return dt;
    }

    public static DataRow GetUyeBilgileri(int UyeId,string dil)
    {
        DataTable dt = dataManager.ExecuteDataTable("GetUyeBilgileri", UyeId,dil);
        return dt.Rows[0];
    }

    public static DataTable CheckUyeVarmi(string EPosta)
    {
        DataTable dt = dataManager.ExecuteDataTable("CheckUyeVarmi", EPosta);
        return dt;
    }

    public static DataTable GetUyeBilgileri(string DogrulamaKodu)
    {
        DataTable dt = dataManager.ExecuteDataTable("UpdUyeAktivasyon", DogrulamaKodu);
        return dt;
    }

    public static DataRow InsUye(string Ad, string Soyad, string Cinsiyet, string Mail, string Parola, bool TumHaberler)
    {
        DataTable dt = dataManager.ExecuteDataTable("InsUye", Ad, Soyad, Cinsiyet, Mail, Parola, TumHaberler);
        return dt.Rows[0];
    }

    public static DataRow UpdUye(int UyeId, string Ad, string Soyad, string Cinsiyet, string EPosta, string Parola, bool TumHaberler)
    {
        DataTable dt = dataManager.ExecuteDataTable("UpdUye", Ad, Soyad, Cinsiyet, EPosta, Parola, UyeId,TumHaberler);
        return dt.Rows[0];
    }

    public static bool InsEPosta(string EPosta,bool Tumu)
    {
        DataTable dt = dataManager.ExecuteDataTable("InsEPosta", EPosta,Tumu);
        return dt.Rows.Count > 0;
    }

    public static void GetLookUp(bool Isrefresh=false)
    {
        if (LookUp.Renk.Rows.Count == 0 || Isrefresh)
        {
            
            DataSet ds = dataManager.ExecuteDataSet("GetLookup",Cookies.Dil);
            LookUp.Renk = ds.Tables[0];
            LookUp.Desen = ds.Tables[1];
            LookUp.AnaKategori = ds.Tables[2];
            LookUp.Kategori = ds.Tables[3];
            LookUp.CantaTuru = ds.Tables[4];
            LookUp.DeriCesidi = ds.Tables[5];
            LookUp.Kampanya = ds.Tables[6];
            LookUp.Kargo = ds.Tables[7];
        }
    }

    public static bool InsUrun(int AnaKategoriId, int KategoriId, int CantaTuruId, int DeriCesidiId, string UrunKod, string UrunAd, string Yukseklik, string Genislik, string Dip, string SapUzunlugu, string OmuzAskisi,
                                int RenkId, int DesenId, decimal Fiyat, decimal IndirimliFiyat,int KampanyaId, int StokMiktari, bool GelecekMi, string UrunDetay, string ResimIsimleri)
    {
        DataTable dt = dataManager.ExecuteDataTable("InsUrun", AnaKategoriId, KategoriId, CantaTuruId, DeriCesidiId, UrunKod, UrunAd, Yukseklik, Genislik, Dip, SapUzunlugu, OmuzAskisi,
                                                                RenkId, DesenId, Fiyat, IndirimliFiyat,KampanyaId, StokMiktari, GelecekMi, UrunDetay, ResimIsimleri, Sessions.UyeId);
        return dt.Rows.Count > 0;
    }

    public static bool UpdUrun(int UrunId, int AnaKategoriId, int KategoriId, int CantaTuruId, int DeriCesidiId, string UrunKod, string UrunAd, string Yukseklik, string Genislik, string Dip, string SapUzunlugu, string OmuzAskisi,
                                int RenkId, int DesenId, decimal Fiyat, decimal IndirimliFiyat, int KampanyaId, int StokMiktari, bool GelecekMi, string UrunDetay, string ResimIsimleri)
    {
        DataTable dt = dataManager.ExecuteDataTable("UpdUrun", UrunId, AnaKategoriId, KategoriId, CantaTuruId, DeriCesidiId, UrunKod, UrunAd, Yukseklik, Genislik, Dip, SapUzunlugu, OmuzAskisi,
                                                                RenkId, DesenId, Fiyat, IndirimliFiyat,KampanyaId, StokMiktari, GelecekMi, UrunDetay, ResimIsimleri, Sessions.UyeId);
        return dt.Rows.Count > 0;
    }

    public static DataTable GetUrunBilgileri(int UrunId,string Dil)
    {
        
        DataTable dt = dataManager.ExecuteDataTable("GetUrun", UrunId,Sessions.UyeId,Dil);
        return dt;
    }

    public static DataRow CantayaEkle(int UrunId, int Adet )
    {
        DataTable dt = dataManager.ExecuteDataTable("InsCanta", UrunId,Sessions.UyeId,Adet);
        return dt.Rows[0];
    }

    public static int CantadanCikar(int UrunId)
    {
        int SepetAdet = Convert.ToInt16(dataManager.ExecuteScaler("DelCanta", UrunId, Sessions.UyeId));
        return SepetAdet;
    }

    public static int UpdSiparisAdedi(int UrunId,int Adet)
    {
        int SepetAdet = Convert.ToInt16(dataManager.ExecuteScaler("UpdSiparisAdet", UrunId, Sessions.UyeId, Adet));
        return SepetAdet;
    }

    public static DataRow GetSiparisOnaylama(int UrunId, int Adet, string Dil)
    {
        DataTable dt = dataManager.ExecuteDataTable("GetSiparisOnaylama", UrunId, Sessions.UyeId,Adet,Dil);
        return dt.Rows[0];
    }

    public static int InsAdres(string AdSoyad,string SahisAdres,string SahisPostaKodu, string EPosta, string FirmaUnvani,string VergiDairesi,string VergiNo,string FirmaAdres,string FirmaPostaKodu,
                                int UlkeId,int SehirId,int IlceId,string Semt,string Telefon,string Telefon2)
    {
        int AdresId = Convert.ToInt16(dataManager.ExecuteScaler("InsAdres", AdSoyad, SahisAdres, SahisPostaKodu,EPosta, FirmaUnvani, VergiDairesi, VergiNo, FirmaAdres, FirmaPostaKodu,
                                    UlkeId, SehirId, IlceId, Semt, Telefon, Telefon2, Sessions.UyeId));
        return AdresId;
    }

    public static DataRow InsSiparis(int OdemeSekli, int FaturaAdresi, int TeslimatAdresi,decimal Fiyat,decimal OdenecekTutar,decimal ParaPuan,
                                    bool ParaPuanKullanildiMi, bool IndirimKullanildiMi, int IndirimId, int ParaBirimiId, string ParaBirimi, int UrunId, int HizliAdet,int SiparisReferansId,bool HediyePaketiMi, string HediyeNotu)
    {       

        DataTable dtSiparis =  dataManager.ExecuteDataTable("InsSiparis", OdemeSekli,  FaturaAdresi,  TeslimatAdresi, Fiyat, OdenecekTutar, ParaPuan,
                                     ParaPuanKullanildiMi, IndirimKullanildiMi, IndirimId, ParaBirimiId, ParaBirimi, UrunId, HizliAdet, Sessions.UyeId, SiparisReferansId, HediyePaketiMi, HediyeNotu);
        return dtSiparis.Rows[0];
    }

    public static int UrunBegen(int UrunId)
    {
        int begenAdet = Convert.ToInt16(dataManager.ExecuteScaler("UrunBegen", UrunId, Sessions.UyeId));
        return begenAdet;
    }

    public static bool StokUyarisiEkle(int UrunId, int UyeId)
    {
        DataTable dtStokUyari = dataManager.ExecuteDataTable("InsStokUyarisi", UrunId, UyeId);
        return dtStokUyari.Rows.Count > 0;
    }

    public static DataTable GetParolaYenileme(string DogrulamaKodu)
    {
        DataTable dt = dataManager.ExecuteDataTable("GetParolaYenileme", DogrulamaKodu);
        return dt;
    }

    public static void DelAdres(int AdresId)
    {
        dataManager.ExecuteNonQuery("DelAdres",AdresId );
    }
    
    public static void Upd3DYanit(int SiparisReferansId, string AuthCode, string Response, string HostRefNum, string ProcReturnCode,
                                    string TransId, string ErrMsg, string ClientIp, string ReturnOid, string MaskedPan, string PaymentMethod, string rnd)
    {
        dataManager.ExecuteNonQuery("Upd3DYanit", SiparisReferansId, AuthCode, Response, HostRefNum, ProcReturnCode,
                                     TransId, ErrMsg, ClientIp, ReturnOid, MaskedPan, PaymentMethod, rnd);
    }

    public static int GetSiparisReferans(int UrunId,decimal OdenecekTutar,int OdemeSekli,int FaturaAdresi,int TeslimatAdresi,decimal Fiyat,
                                        decimal ParaPuan,bool ParaPuanKullanildiMi,bool IndirimKullanildiMi,int IndirimId,int ParaBirimiId,string ParaBirimi,int HizliAdet)
    {
 	    int OrderReferansId = dataManager.ExecuteScaler("GetSiparisReferans", Sessions.UyeId, UrunId, OdenecekTutar,OdemeSekli, FaturaAdresi, TeslimatAdresi, Fiyat,
                                         ParaPuan, ParaPuanKullanildiMi, IndirimKullanildiMi, IndirimId, ParaBirimiId, ParaBirimi, HizliAdet).ToIntNN();
        return OrderReferansId;
    }

    public static void InsYorum(string Yorum, int UrunId)
    {
        dataManager.ExecuteNonQuery("InsYorum", Yorum, UrunId, Sessions.UyeId);
    }

    public static bool InsUyeMesaj(string Ad, string Soyad, string Telefon, string EPosta, string Mesaj, string MesajTuru)
    {
        DataTable dt = dataManager.ExecuteDataTable("InsUyeMesaj", Ad, Soyad, Telefon, EPosta, Mesaj, MesajTuru, Sessions.UyeId);
        return dt.Rows.Count > 0;
    }

    public static string GetMetin(int Tip)
    {
        DataTable dt = dataManager.ExecuteDataTableByQuery("Select Aciklama from MusteriHizmetleri where MetinId = " + Tip.ToString());
        if (dt.Rows.Count > 0)
            return dt.Rows[0]["Aciklama"].ToString();
        else
            return "";
    }
}